package com.task.controller.executor.impl;

import com.task.controller.annotation.UITask;
import com.task.controller.entity.Mission;
import com.task.controller.executor.Executor;
import com.task.controller.manager.impl.UIMethodManager;
import com.task.controller.param.ParamParser;
import ohos.app.Context;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;
import java.util.NoSuchElementException;

public class UIExecutor implements Executor {

    private static UIExecutor uiExecutor = new UIExecutor();

    private UIExecutor() {
        super();
    }

    public static UIExecutor getInstance() {
        if (uiExecutor == null) {
            uiExecutor = new UIExecutor();
        }
        return uiExecutor;
    }

    @Override
    public void execute(Context context, String resource, Object o, ExecuteEvent executeEvent) {
        Thread thread = Thread.currentThread();
        String name = thread.getName();

        Mission mission = UIMethodManager.getInstance().lookup(resource);
        if (mission == null) {
            throw new NoSuchElementException("没有对应的资源");
        }
        Method method = mission.getMethod();
        ParamParser parse = mission.getParse();
        Object instance = mission.getObject();
        Object obj = parse.parse(context, o, method);
        method.setAccessible(true);
        if (!"main".equals(name) || method.getDeclaredAnnotation(UITask.class).isAsync()) {
            context.getUITaskDispatcher().asyncDispatch(() -> {
                Object object = null;
                try {
                    if (obj == null) {
                        method.invoke(instance);
                    } else if (obj instanceof List) {
                        method.invoke(instance, ((List<?>) obj).toArray());
                    } else if (obj instanceof Object[]) {
                        method.invoke(instance, (Object[]) obj);
                    } else {
                        method.invoke(instance, obj);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if (executeEvent != null) {
                    executeEvent.executeListener(object);
                }
            });
        } else {
            Object object = null;
            try {
                if (obj == null) {
                    object = method.invoke(instance);
                } else if (obj instanceof List) {
                    object = method.invoke(instance, ((List<?>) obj).toArray());
                } else if (obj instanceof Object[]) {
                    object = method.invoke(instance, (Object[]) obj);
                } else {
                    object = method.invoke(instance, obj);
                }
            } catch (IllegalAccessException | InvocationTargetException e) {
                e.printStackTrace();
            }
            if (executeEvent != null) {
                executeEvent.executeListener(object);
            }
        }
    }
}
